package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.Calendar;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.OPH_KlasseEinsDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.HonorarTBL;

/**
 * Session Bean implementation class OPH_KlasseEinsDAO
 */
@Stateless
@Local( { OPH_KlasseEinsDAOInt.class })
public class OPH_KlasseEinsDAO implements OPH_KlasseEinsDAOInt {

	@PersistenceContext(unitName = "gynaekomedic-ejbPU")
	private EntityManager	em	= null;
	
    /**
     * Default constructor. 
     */
    public OPH_KlasseEinsDAO() {
        // TODO Auto-generated constructor stub
    }

    
    @Override
	public void delete(Long billnumber)
	{
    	HonorarTBL oph = em.find(HonorarTBL.class, billnumber);

		CustomerTBL cust = oph.getCustomer();

		cust.getOpHonorar().remove(oph);

		em.remove(oph);
		em.flush();

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HonorarTBL> findAvailable_ERLEDIGTE_BillsByCustomer(
			String matricule)
	{
		try
		{
			return (List<HonorarTBL>) em.createQuery(
					"select bi from HonorarTBL bi where customer ='" + matricule
							+ "' and bezahlt = true and erstellt = true and HONORAR_KLASSE= '1' ")
					.getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("An -ERLEDIGTE- OP Klasse 1 Rechnung don't exist", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HonorarTBL> findAvailable_ERSTELLTE_BillsByCustomer(
			String matricule)
	{
		try
		{
			return (List<HonorarTBL>) em
					.createQuery(
							"select bi from HonorarTBL bi where erstellt = true and bezahlt = false and customer =' " 
									+ matricule + "' and HONORAR_KLASSE= '1'").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("An -ERSTELLTE- OP Klasse 1 Rechnung don't exist", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HonorarTBL> findAvailable_NICHTERSTELLTE_Bills()
	{
		try
		{
			return (List<HonorarTBL>) em
					.createQuery(
							"select bi from HonorarTBL bi where erstellt = false and bezahlt = false and HONORAR_KLASSE= '1'")
					.getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException(
					"A -NICHTERSTELLTE- OP Klasse 1 Rechnung don't exist", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HonorarTBL> findAvailable_ACTUAL_BillsByCustomer(String matricule)
	{
		try
		{
			return (List<HonorarTBL>) em
					.createQuery(
							"select bi from HonorarTBL bi where bezahlt = false and erstellt = true and customer ='"
									+ matricule + "' and HONORAR_KLASSE= '1'").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("An -ACTUAL- OP Klasse 1 Rechnung don't exist", e);
		}
	}

	@Override
	public HonorarTBL findBillsByID(Long billNumber)
	{
		return (HonorarTBL) em.find(HonorarTBL.class, billNumber);
	}

	@Override
	public void persist(HonorarTBL todo)
	{
		em.persist(todo);
		em.flush();

	}

	@Override
	public void update(HonorarTBL todo)
	{
		em.merge(todo);
		em.flush();

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HonorarTBL> findBillsNewerThanOf(String matricule, Calendar date)
	{
		try
		{
			return (List<HonorarTBL>) em.createQuery(
					"select b from HonorarTBL b where erstell_datum > '" + date
							+ "'" + " and customer ='" + matricule + "' and HONORAR_KLASSE= '1'")
					.getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("Rechnungen OP Klasse 1:: Newer than " + date
					+ "don't exist", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<HonorarTBL> findBillsOlderThanOf(String matricule, Calendar date)
	{
		try
		{
			return (List<HonorarTBL>) em.createQuery(
					"select b from HonorarTBL b where erstell_datum <= '" + date
							+ "'" + " and customer ='" + matricule + "' and HONORAR_KLASSE= '1'")
					.getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("Rechnungen klasse 1 :: older than " + date
					+ "don't exist", e);
		}
	}

	@Override
	public Long findDatabaseSubPrimaryKeyIndex()
	{
		try
		{

			Long result = (Long) em.createQuery(
					"select max(rechnung_nummer) from HonorarTBL bi ")
					.getSingleResult();

			return result.longValue();
		}
		catch (Exception e)
		{
			// throw new RuntimeException("BillsTbl is empty", e);
			return -1l;
		}
	}
}
